﻿using System;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Alipay.AopSdk.Core.Util
{
	/// <summary>
	/// AOP系统工具类。
	/// </summary>
	public abstract class AopUtils
	{
		/// <summary>
		/// AES加密
		/// </summary>
		/// <param name="bizContent"></param>
		/// <param name="charset"></param>
		/// <param name="encryptKey"></param>
		/// <returns></returns>
		public static string AesEncrypt(string encryptKey, string bizContent, string charset)
		{
			return AlipayEncrypt.AesEncrypt(encryptKey, bizContent, charset);
		}

		/// <summary>
		///     给AOP请求签名。
		///     <remarks>建议使用<code>AlipaySignature.RSASign(parameters, privateKeyPem)</code>></remarks>
		/// </summary>
		/// <param name="parameters">所有字符型的AOP请求参数</param>
		/// <param name="privateKeyPem">签名密钥</param>
		/// <returns>签名</returns>
		public static string SignAopRequest(IDictionary<string, string> parameters, string privateKeyPem, string charset,
			string signType)
		{
			return AlipaySignature.RSASign(parameters, privateKeyPem, charset, signType);
		}

		public static string SignAopRequest(IDictionary<string, string> parameters, string privateKeyPem, string charset,
			bool keyFromFile, string signType)
		{
			return AlipaySignature.RSASign(parameters, privateKeyPem, charset, keyFromFile, signType);
		}


		/// <summary>
		///     清除字典中值为空的项。
		/// </summary>
		/// <param name="dict">待清除的字典</param>
		/// <returns>清除后的字典</returns>
		public static IDictionary<string, T> CleanupDictionary<T>(IDictionary<string, T> dict)
		{
			IDictionary<string, T> newDict = new Dictionary<string, T>(dict.Count);
			var dem = dict.GetEnumerator();

			while (dem.MoveNext())
			{
				var name = dem.Current.Key;
				var value = dem.Current.Value;
				if (value != null)
					newDict.Add(name, value);
			}

			return newDict;
		}

		/// <summary>
		///     获取文件的真实后缀名。目前只支持JPG, GIF, PNG, BMP四种图片文件。
		/// </summary>
		/// <param name="fileData">文件字节流</param>
		/// <returns>JPG, GIF, PNG or null</returns>
		public static string GetFileSuffix(byte[] fileData)
		{
			if (fileData == null || fileData.Length < 10)
				return null;

			if (fileData[0] == 'G' && fileData[1] == 'I' && fileData[2] == 'F')
				return "GIF";
			if (fileData[1] == 'P' && fileData[2] == 'N' && fileData[3] == 'G')
				return "PNG";
			if (fileData[6] == 'J' && fileData[7] == 'F' && fileData[8] == 'I' && fileData[9] == 'F')
				return "JPG";
			if (fileData[0] == 'B' && fileData[1] == 'M')
				return "BMP";
			return null;
		}

		/// <summary>
		///     获取文件的真实媒体类型。目前只支持JPG, GIF, PNG, BMP四种图片文件。
		/// </summary>
		/// <param name="fileData">文件字节流</param>
		/// <returns>媒体类型</returns>
		public static string GetMimeType(byte[] fileData)
		{
			var suffix = GetFileSuffix(fileData);
			string mimeType;

			switch (suffix)
			{
				case "JPG":
					mimeType = "image/jpeg";
					break;
				case "GIF":
					mimeType = "image/gif";
					break;
				case "PNG":
					mimeType = "image/png";
					break;
				case "BMP":
					mimeType = "image/bmp";
					break;
				default:
					mimeType = "application/octet-stream";
					break;
			}

			return mimeType;
		}

		/// <summary>
		///     根据文件后缀名获取文件的媒体类型。
		/// </summary>
		/// <param name="fileName">带后缀的文件名或文件全名</param>
		/// <returns>媒体类型</returns>
		public static string GetMimeType(string fileName)
		{
			string mimeType;
			fileName = fileName.ToLower();

			if (fileName.EndsWith(".bmp", StringComparison.CurrentCulture))
				mimeType = "image/bmp";
			else if (fileName.EndsWith(".gif", StringComparison.CurrentCulture))
				mimeType = "image/gif";
			else if (fileName.EndsWith(".jpg", StringComparison.CurrentCulture) ||
			         fileName.EndsWith(".jpeg", StringComparison.CurrentCulture))
				mimeType = "image/jpeg";
			else if (fileName.EndsWith(".png", StringComparison.CurrentCulture))
				mimeType = "image/png";
			else
				mimeType = "application/octet-stream";

			return mimeType;
		}

		/// <summary>
		///     根据API名称获取响应根节点名称。
		/// </summary>
		/// <param name="api">API名称</param>
		/// <returns></returns>
		public static string GetRootElement(string api)
		{
			var pos = api.IndexOf(".");
			if (pos != -1 && api.Length > pos)
				api = api.Substring(pos + 1).Replace('.', '_');
			return api + "_response";
		}

		public static IDictionary ParseJson(string body)
		{
			return JsonConvert.DeserializeObject<IDictionary>(body);
		}
	}
}